﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Student 的摘要说明
/// </summary>
public class Student: LwUser
{
    public const string StrStudent = "student";
    public Paper Pap;
    public int DoctorGuid;
    public int ExpertGuid;
    public string PaperName;
    public int PaperFlag;
    public int CategoryGuid;
	public Student(int g):base(g)
	{
        this.Init();
    }
    public Student(string uid)
        : base(uid)
    {
        this.Init();
    }

    public void Init()
    {
        string sql = "select * from Student where UserGuid="+this.Guid;
        DataRow dr = Fun.CreateDr(sql);
        this.DoctorGuid = Convert.ToInt32(dr["DoctorGuid"]);
        this.ExpertGuid = Convert.ToInt32(dr["ExpertGuid"]);
        this.PaperFlag = Convert.ToInt32(dr["PaperFlag"]);
        this.PaperName = dr["PaperName"].ToString().Trim();
        this.CategoryGuid = Convert.ToInt32(dr["CategoryGuid"]);
    }

    public string DoctorName
    {
        get
        {
            string sql = "select RealName as n from Users where Guid=" + this.DoctorGuid.ToString();
            return Fun.GetField(sql).ToString().Trim();
        }
    }

    public string ExpertName
    {
        get
        {
            if (this.ExpertGuid <= 0)
            {
                return "-";
            }
            string sql = "select RealName as n from Users where Guid="+this.ExpertGuid.ToString();
            return Fun.GetField(sql).ToString().Trim();
        }
    }

    public int TeamGuid
    {
        get
        {
            string sql = "select TeamGuid as n from Doctor where UserGuid="+this.DoctorGuid.ToString();
            return Convert.ToInt32(Fun.GetField(sql));
        }
    }

    public DataTable RecversDt()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("value", Params.IntType);
        dt.Columns.Add("text", Params.StrType);
        DataRow dr;
        dr = dt.NewRow();
        dr["text"] = this.DoctorName;
        dr["value"] = this.DoctorGuid;
        dt.Rows.Add(dr);
        if (this.ExpertGuid != 0)
        {
            dr = dt.NewRow();
            dr["text"] = "评审专家";
            dr["value"] = this.ExpertGuid;
            dt.Rows.Add(dr);
        }
        return dt;
    }

    public bool HasDoctor(int dg)
    {
        return Doctor.DoctorHasStudent(dg, this.Guid);
    }

    public bool HasExpert(int eg)
    {
        return Doctor.ExpertHasStudent(eg, this.Guid);
    }

    public static int Count()
    {
        string sql = "select count(*) as n from Student";
        return Convert.ToInt32(Fun.GetField(sql, "n"));
    }

    public static int PastCount()  //论文通过1
    {
        string sql = "select count(*) as n from Student where PaperFlag="+Paper.PastFlag.ToString()+" or PaperFlag="+Paper.AcceptFlag.ToString();
        return Convert.ToInt32(Fun.GetField(sql, "n"));
    }

    public static int SuspendCount()  //论文在审
    {
        string sql = "select count(*) as n from Student where PaperFlag="+Paper.SuspendFlag.ToString();
        return Convert.ToInt32(Fun.GetField(sql, "n"));
    }

    public static int FailCount() //论文打回
    {
        string sql = "select count(*) as n from Student where PaperFlag="+Paper.FailFlag.ToString();
        return Convert.ToInt32(Fun.GetField(sql, "n"));
    }

    public static int PaperCount()  
    {
        string sql = "select count(*) as n from Student where PaperFlag<>"+Paper.NoneFlag.ToString();
        return Convert.ToInt32(Fun.GetField(sql, "n"));
    }

    public static Student Check()
    {
        try
        {
            LwUser lu = (LwUser)System.Web.HttpContext.Current.Session["Loginer"];
            string uid = System.Web.HttpUtility.UrlDecode(System.Web.HttpContext.Current.Request.Params["uid"]);
            if (lu.UserType == LwUser.AdminType)
            {
                return new Student(uid);
            }
            if (lu.UserType == LwUser.StudentType)
            {
                if (lu.UserID != uid)
                {
                    return null;
                }
                return new Student(uid);
            }
            return null;
        }
        catch
        {
            return null;
        }
    }

    public static bool IsStudent(int g)
    {
        string sql = "select UserType from Users where Guid=" + g.ToString();
        object obj = Fun.GetField(sql, "UserType");
        if (obj == null)
        {
            return false;
        }
        return obj.ToString().Trim() == LwUser.StudentType;
    }

    public static int Add(string uid, string n, string email, int dg, ref string tip)
    {
        int g = LwUser.Add(uid, n, email, LwUser.StudentType, ref tip);
        if (g == 0)
        {
            return 0;
        }
        string sql = "insert into Student(Guid,UserGuid,DoctorGuid) values("+Fun.Guid().ToString()+","+g.ToString() +","+dg.ToString()+ ")";
        if (Fun.Execute(sql) > 0)
        {
            return g;
        }
        else
        {
            tip = "添加失败！";
            sql = "delete from Users where Guid=" + g.ToString();
            Fun.Execute(sql);
            return 0;
        }
    }

    public static int Add(string uid, string n, string email, string d, ref string tip)
    {
        string sql = "select Guid from Users where UserType='"+LwUser.DoctorType+"' and UserID='"+d.Replace("'","''")+"'";
        DataTable dt = Fun.CreateDt(sql);
        if (dt.Rows.Count < 1)
        {
            tip = "不存在ID为"+d+"的导师";
            return 0;
        }
        int dg = Convert.ToInt32(dt.Rows[0][0]);
        return Student.Add(uid, n, email, dg, ref tip);
    }

    public static void Delete(int g)
    {
        Paper.Delete(g);
        string sql = "delete from Student where UserGuid="+g.ToString();
        Fun.Execute(sql);
        LwUser.Delete(g);
    }

    public override bool Update()
    {
        string sql = "update Student set PaperFlag="+this.PaperFlag.ToString()+",PaperName='"+Fun.L(this.PaperName)+"',CategoryGuid="+this.CategoryGuid.ToString()+",DoctorGuid="+this.DoctorGuid.ToString()+",ExpertGuid="+this.ExpertGuid.ToString()+" where UserGuid="+this.Guid.ToString();
        Fun.Execute(sql);
        return base.Update();
    }
}